package cn.hx.mall.sell.controller;

import cn.hx.mall.sell.entity.FlashPromotion;
import cn.hx.mall.sell.service.FlashPromotionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 限时购表(FlashPromotion)表控制层
 *
 * @author makejava
 * @since 2022-11-02 16:10:27
 */
@Controller
@RequestMapping("/sell/flashPromotion")
public class FlashPromotionController {
    /**
     * 服务对象
     */
    @Autowired
    private FlashPromotionService flashPromotionService;


    /**
     * 秒杀活动列表
     *
     * @param map 列表页面数据模型
     * @param page 当前页（默认1）
     * @param size 每页大小（默认10）
     * @return  列表页面
     */
    @GetMapping("/list")
    public String flashPromotionList(ModelMap map, FlashPromotion flashPromotion,
                                     @RequestParam(value = "page",defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<FlashPromotion> flashPromotions = PageHelper.startPage(page, size);
        flashPromotionService.queryAll(flashPromotion);
        PageInfo<FlashPromotion> pageInfo = new PageInfo<>(flashPromotions);
        map.addAttribute("pageInfo", pageInfo);
        map.addAttribute("search", flashPromotion.getTitle());
        return "sell/flashPromotionList";
    }

    /**
     * 修改状态
     *      该方法已被取代，因为会导致页面端出现刷新跳转的抖动，用户体验不好
     *
     * @param flashPromotion 包含主键id 和 当前状态
     * @return  重定向到秒杀活动列表
     * @since v1.0
     */
    @Deprecated
    @GetMapping("/status")
    public String status(FlashPromotion flashPromotion,
                         @RequestParam(value = "page",defaultValue = "1") Integer page,
                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Integer curr = flashPromotion.getStatus();
        if (curr == 1) {
            flashPromotion.setStatus(0);
        }else {
            flashPromotion.setStatus(1);
        }
        flashPromotionService.update(flashPromotion);
        return "redirect:/sell/flashPromotion/list?title=" + flashPromotion.getTitle() + "&page=" + page + "&size=" + size;
    }

    /**
     * 状态修改
     *
     * @param flashPromotion  需要修改的活动(id)
     * @return  修改后的状态
     */
    @PostMapping("/change")
    @ResponseBody
    public Map<String, Object> changeStatus(FlashPromotion flashPromotion) {
        Long id = flashPromotion.getId();
        Integer curr = flashPromotionService.queryById(id).getStatus();
        if (curr == 1) {
            flashPromotion.setStatus(0);
        }else {
            flashPromotion.setStatus(1);
        }
        FlashPromotion update = flashPromotionService.update(flashPromotion);
        Map<String, Object> status = new HashMap<>();
        status.put("id", update.getId());
        status.put("status", update.getStatus());
        return status;
    }

    /**
     * 根据id查询活动信息（用于回显）
     *
     * @param id 主键
     * @return 查询到的数据
     */
    @GetMapping("/queryById/{id}")
    @ResponseBody
    public Map<String, Object> queryById(@PathVariable Long id) {
        FlashPromotion flashPromotion = flashPromotionService.queryById(id);
        Map<String, Object> resMap = new HashMap<>(1);
        resMap.put("data", flashPromotion);
        return resMap;
    }

    /**
     * 保存新增或保存修改
     *
     * @param flashPromotion    需要保存的信息
     * @param page  当前页面所在页
     * @param size  当前页面每页展示条数
     * @return  重定向到秒杀活动列表
     */
    @PostMapping("/save")
    public String save(FlashPromotion flashPromotion,
                       @RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (flashPromotion.getId() == null) {
            // 没有id，是添加
            flashPromotionService.insert(flashPromotion);
        }else {
            flashPromotionService.update(flashPromotion);
        }
        return "redirect:/sell/flashPromotion/list?page=" + page + "&size=" + size;
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return  重定向到秒杀活动列表
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id,
                         @RequestParam(value = "page",defaultValue = "1") Integer page,
                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        System.out.println("删除==> id == " + id);
        flashPromotionService.deleteById(id);
        return "redirect:/sell/flashPromotion/list?page=" + page + "&size=" + size;
    }
}